<template>
  <div class="public-homepage app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-row :gutter="24" style="margin: 0">
        <el-col :span="20">
          <el-form-item prop="imgType">
            <el-select
              v-model="queryParams.imgType"
              placeholder="类型"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in typeList"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="imgStatus">
            <el-select
              v-model="queryParams.imgStatus"
              placeholder="状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option label="停用" :value="0" />
              <el-option label="启用" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button
              size="mini"
              :style="{ color: '#3370ff', borderColor: '#3370ff' }"
              @click="handleQuery"
              >查询</el-button
            >
            <el-button size="mini" @click="resetQuery">清空</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="end">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="0.5"> </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="类型"
        align="center"
        prop="imgTypeName"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="imgStatus" width="180">
        <template slot-scope="scope">
          <div class="hpStatusStart" v-if="scope.row.imgStatus == '1'">
            <div class="icon">
              <i class="el-icon-check"></i>
            </div>
            启用
          </div>
          <div class="hpStatusEnd" v-else>
            <div class="icon">
              <i class="el-icon-close"></i>
            </div>
            停用
          </div>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="loginTime" width="180">
        <template slot-scope="scope">
          <div class="sortBox">
            <i
              @click="moveFn(scope.row.id, 'top')"
              style="color: #3370ff; transform: rotate(180deg)"
              class="el-icon-download"
            ></i>
            <i
              @click="moveFn(scope.row.id, 'up')"
              style="color: #00c3e1"
              class="el-icon-top"
            ></i>
            <i
              @click="moveFn(scope.row.id, 'down')"
              style="color: #f8d540"
              class="el-icon-bottom"
            ></i>
            <i
              @click="moveFn(scope.row.id, 'bottom')"
              style="color: #f96906"
              class="el-icon-download"
            ></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 'detail')"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 'edit')"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: #f54a45"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, deleteFn, exportFn, move } from "@/api/publicService/homepage";

export default {
  name: "Logininfor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 状态数据字典
      statusOptions: [],
      typeList: [],
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "loginTime", order: "descending" },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imgStatus: undefined,
        imgType: undefined,
        startTime: undefined,
        endTime: undefined,
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("shouye_img_type").then((response) => {
      this.typeList = response.data;
    });
  },
  methods: {
    /** 新增 */
    handleAdd() {
      this.$router.push({
        path: "/publicService/homepage/action",
        query: {
          action: "add",
        },
      });
    },
    moveFn(id, type) {
      move({
        id,
        type,
      }).then((res) => {
        if (res?.code === 200) {
          this.msgSuccess("移动成功");
          this.getList();
        }
      });
    },
    handleUpdate(row, action) {
      let obj = {
        startTime: this.dateRange?.[0] || undefined,
        endTime: this.dateRange?.[1] || undefined,
      };
      this.$router.push({
        path: "/publicService/homepage/action",
        query: {
          action,
          id: row.id,
          ...this.queryParams,
          ...this.obj,
        },
      });
    },
    getList() {
      this.loading = true;
      let obj = {
        startTime: this.dateRange?.[0] || undefined,
        endTime: this.dateRange?.[1] || undefined,
      };
      list({
        ...this.queryParams,
        ...obj,
      }).then((response) => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    roomPage(row) {
      this.$router.push({
        path: "/publicService/stayManage/room",
        query: {
          id: row.id,
        },
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除该数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteFn(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportFn();
        })
        .then((res) => {
          if (res.code === 200 && res.data) {
            const link = document.createElement("a");
            link.href = res.data;
            link.download = "file-name.ext";
            link.click();
          }
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>


<style rel="stylesheet/scss" lang="scss">
.public-homepage {
  .sortBox {
    i {
      font-size: 14px;
      margin-left: 10px;
      cursor: pointer;
    }
  }
  .hpStatusEnd,
  .hpStatusStart {
    margin: 0 auto;
    width: 70px;
    background: #d6e2ff;
    border-radius: 2px 2px 2px 2px;
    padding: 0 10px;
    line-height: 24px;
    height: 24px;
    font-family: HarmonyOS Sans SC-Regular, HarmonyOS Sans SC;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #245bdb;
    .icon {
      height: 16px;
      width: 16px;
      border-radius: 50%;
      margin-right: 6px;
      background-color: #245bdb;
      line-height: 16px;
      font-size: 12px;
      color: #fff;
    }
  }
  .hpStatusEnd {
    color: #db6a24;
    background-color: #ffe8d6;
    .icon {
      background-color: #db6a24;
    }
  }
}
</style>